package cv.gzd.robot.interceptor;

import cv.gzd.robot.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @Author: Jun
 * @Date: 2018/8/28 14:18
 */
public class AuthorInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        String requestURI = httpServletRequest.getRequestURI();
        if (requestURI.contains("user")) {
            User user = (User) httpServletRequest.getSession().getAttribute("USER_SESSION");
            if (user.getAuthorization().equals("管理员") || user.getAuthorization().equals("超级管理员")) {
                return true;
            }
        }
        PrintWriter writer = httpServletResponse.getWriter();
        writer.print("<meta charset=\"UTF-8\"><script> alert('您不是管理员，无法进入用户管理！');window.location='/index.action';  </script>");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
